/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entidades.Cardapio;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author usuario
 */
public class CardapioSemanaControlador {

    private EntityManagerFactory emf;

    public CardapioSemanaControlador() {
        emf = Persistence.createEntityManagerFactory("restaurantePU");
    }

    public List<Object[]> getCardapioSemanal() {

        EntityManager em = getEntityManager();

        List<Object[]> lista = new ArrayList<Object[]>();

        Object[] segunda = new Object[]{"seg",new ArrayList<Cardapio>()};
        Object[] terca = new Object[]{"ter",new ArrayList<Cardapio>()};
        Object[] quarta = new Object[]{"qua",new ArrayList<Cardapio>()};
        Object[] quinta = new Object[]{"qui",new ArrayList<Cardapio>()};
        Object[] sexta = new Object[]{"sex",new ArrayList<Cardapio>()};
        Object[] sabado = new Object[]{"sab",new ArrayList<Cardapio>()};
        Object[] domingo = new Object[]{"dom",new ArrayList<Cardapio>()};

         try {

            List<Cardapio> list = em.createNamedQuery("Cardapio.findCardapioSemanal")
                    .setParameter("semanal", true)
                    .getResultList();

            for (Cardapio cardapio : list) {


                if(cardapio.isSemanal()){
                    String dia = cardapio.getDiaSemana().toLowerCase();

                    if (dia.equals("seg")) {
                        ((ArrayList<Cardapio>)segunda[1]).add(cardapio);
                    } else if (dia.equals("ter")) {
                        ((ArrayList<Cardapio>)terca[1]).add(cardapio);
                    } else if (dia.equals("qua")) {
                        ((ArrayList<Cardapio>)quarta[1]).add(cardapio);
                    } else if (dia.equals("qui")) {
                        ((ArrayList<Cardapio>)quinta[1]).add(cardapio);
                    } else if (dia.equals("sex")) {
                        ((ArrayList<Cardapio>)sexta[1]).add(cardapio);
                    } else if (dia.equals("sab")) {
                        ((ArrayList<Cardapio>)sabado[1]).add(cardapio);
                    } else if (dia.equals("dom")) {
                        ((ArrayList<Cardapio>)domingo[1]).add(cardapio);
                    }
                }
            }

            if(((ArrayList<Cardapio>)segunda[1]).size() != 0)
                lista.add(segunda);
            if(((ArrayList<Cardapio>)terca[1]).size() != 0)
                lista.add(terca);
            if(((ArrayList<Cardapio>)quarta[1]).size() != 0)
                lista.add(quarta);
            if(((ArrayList<Cardapio>)quinta[1]).size() != 0)
                lista.add(quinta);
            if(((ArrayList<Cardapio>)sexta[1]).size() != 0)
                lista.add(sexta);
            if(((ArrayList<Cardapio>)sabado[1]).size() != 0)
                lista.add(sabado);
            if(((ArrayList<Cardapio>)domingo[1]).size() != 0)
                lista.add(domingo);
            
        } catch (RuntimeException re) {
            re.printStackTrace();
        } finally {
            em.close();
        }
        return lista;
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
}
